package com.demo.connectdbtest.module.tool.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.demo.connectdbtest.module.common.util.DateUtils;
import com.demo.connectdbtest.module.tool.domain.OmSwitchPtInfo;
import com.demo.connectdbtest.module.tool.enums.LightTypeEnum;
import com.demo.connectdbtest.module.tool.popoMapper.OmDeviceInfoMapper;
import com.demo.connectdbtest.module.tool.popoMapper.OmSwitchPtInfoMapper;
import com.demo.connectdbtest.module.tool.service.ToolService;
import com.demo.connectdbtest.module.tool.vo.ActiveTarget;
import com.demo.connectdbtest.module.tool.vo.LightEnablePromDataDTO;
import com.demo.connectdbtest.module.tool.vo.NodeExporterLabel;
import com.demo.connectdbtest.module.tool.vo.NodeExporterTargetResp;
import com.demo.connectdbtest.module.tool.vo.OmDeviceInfo;
import com.demo.connectdbtest.module.tool.vo.PromDataDTO;
import com.demo.connectdbtest.module.tool.vo.PromRespLightEnableResult;
import com.demo.connectdbtest.module.tool.vo.PromRespResult;
import com.demo.connectdbtest.module.tool.vo.StatisticDeviceDataDTO;
import com.demo.connectdbtest.module.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

@Service
public class ToolServiceImpl implements ToolService {

    @Autowired
    private OmDeviceInfoMapper omDeviceInfoMapper;
    @Autowired
    private OmSwitchPtInfoMapper omSwitchPtInfoMapper;

    //设备在线监控
    @Override
    public List<Object> statisticDeviceStatusMonitor(StatisticDeviceDataDTO dto) {
        List<Object> respList = new ArrayList<>();


        //获取设备信息表的数据
        List<OmDeviceInfo> omDeviceInfos = omDeviceInfoMapper.selectAll();
        if (CollectionUtils.isEmpty(omDeviceInfos)) {
            return respList;
        }

        List<PromRespResult> handleList = new ArrayList<>();
        HashMap<String, List<PromRespResult>> handlerMap = new HashMap<>();
        List<String> statList = dto.getStatList();
        for (String item : statList) {
            //item = "{\"status\":\"success\",\"data\":{\"resultType\":\"matrix\",\"result\":[{\"metric\":{\"__name__\":\"up\",\"address\":\"10.252.176.74:9116\",\"devicename\":\"汇聚交换机\",\"instance\":\"10.52.116.1\",\"job\":\"snmp-exporter\",\"line_name\":\"十三号线\",\"station_code\":\"1310\",\"station_id\":\"293\",\"station_name\":\"官湖\",\"tenderSectionName\":\"E\"},\"values\":[[1721318400,\"0\"]]},{\"metric\":{\"__name__\":\"up\",\"address\":\"10.252.176.75:9116\",\"devicename\":\"汇聚交换机\",\"instance\":\"10.55.79.118\",\"job\":\"snmp-exporter\",\"line_name\":\"十三号线\",\"station_code\":\"1311\",\"station_id\":\"294\",\"station_name\":\"新沙\",\"tenderSectionName\":\"E\"},\"values\":[[1721318400,\"0\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.171.20:9100\",\"job\":\"node_exporter\",\"server\":\"开发平台服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.171.50:9100\",\"job\":\"node_exporter\",\"server\":\"存储服务ceph-01\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.113:9100\",\"job\":\"node_exporter\",\"server\":\"轨道交通-mysql8\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.15:9100\",\"job\":\"node_exporter\",\"server\":\"业务开发部-日志文件存储\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.17:19100\",\"job\":\"node_exporter\",\"server\":\"oracle-server k8s开发\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.18:19100\",\"job\":\"node_exporter\",\"server\":\"redis哨兵集群redis-sentinel-02\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.19:19100\",\"job\":\"node_exporter\",\"server\":\"redis哨兵集群redis-sentinel-03\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.22:9100\",\"job\":\"node_exporter\",\"server\":\"安检部使用-雷朋川2\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.24:9100\",\"job\":\"node_exporter\",\"server\":\"安检部使用-雷朋川1\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.2:9100\",\"job\":\"node_exporter\",\"server\":\"电子发票测试环境\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.30:9100\",\"job\":\"node_exporter\",\"server\":\"南大数据库-gbase\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.42:19100\",\"job\":\"node_exporter\",\"server\":\"测试部-测试环境\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.45:19100\",\"job\":\"node_exporter\",\"server\":\"nacos\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.46:9100\",\"job\":\"node_exporter\",\"server\":\"安检服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.50:19100\",\"job\":\"node_exporter\",\"server\":\"redis哨兵集群redis-sentinel-01\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.53:9100\",\"job\":\"node_exporter\",\"server\":\"智慧收益项目-帆软01\"},\"values\":[[1721318400,\"0\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.72:9100\",\"job\":\"node_exporter\",\"server\":\"智慧安检应用服务器-2\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.73:9100\",\"job\":\"node_exporter\",\"server\":\"智慧安检应用服务器-3\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.75:9100\",\"job\":\"node_exporter\",\"server\":\"智慧安检存储服务器-1\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.21.12:9100\",\"job\":\"node_exporter\",\"server\":\"大数据测试机-3(外部联调环境)\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.39.11:9100\",\"job\":\"node_exporter\",\"server\":\"elk日志与监控服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.39.18:9100\",\"job\":\"node_exporter\",\"server\":\"智慧安检集中判图项目\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.39.23:9100\",\"job\":\"node_exporter\",\"server\":\"安检部人脸项目使用-20210318\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.10:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.11:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.12:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.13:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.14:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.15:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.16:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.17:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.18:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"0\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.19:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"0\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.2:19100\",\"job\":\"node_exporter\",\"line_name\":\"十八号线\",\"server\":\"sims-jg01,sims-rf01,sims-sim01\",\"station_code\":\"9918\",\"station_id\":\"9918\",\"station_name\":\"横沥\",\"tenderSectionName\":\"E\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.3:19100\",\"job\":\"node_exporter\",\"line_name\":\"十三号线\",\"server\":\"sims-jg02,sims-im03\",\"station_code\":\"1308\",\"station_id\":\"291\",\"station_name\":\"白江\",\"tenderSectionName\":\"E\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.6:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.7:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.8:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.9:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]}]}}";
//            String path = JsonUtil.class.getClassLoader().getResource("stat_device_num_by_type.json").getPath();
//            String path = JsonUtil.class.getClassLoader().getResource("all-device-status-2024-08-08-05-30.json").getPath();
            String path = JsonUtil.class.getClassLoader().getResource("deivice-monitor-2024-08-13-21-00-00.json").getPath();
            item = JsonUtil.readJsonFile(path);

            PromDataDTO parseData = JSONObject.parseObject(item, PromDataDTO.class);
            System.out.println("解析后的json数据");

            //匹配ip的数据才统计处理
            if (Objects.nonNull(parseData.getData()) && !CollectionUtils.isEmpty(parseData.getData().getResult())) {
                List<PromRespResult> result = parseData.getData().getResult();
                for (PromRespResult promRespResult : result) {
                    if (Objects.nonNull(promRespResult.getMetric()) && !StringUtils.isEmpty(promRespResult.getMetric().getInstance())) {
                        String instance = promRespResult.getMetric().getInstance();
                        if (StringUtils.isEmpty(instance)) {
                            continue;
                        }
                        boolean contains = instance.contains(":");
                        if (contains) {
                            instance = promRespResult.getMetric().getInstance().split(":")[0];
                        }
                        String finalInstance = instance;
                        List<OmDeviceInfo> collect = omDeviceInfos.stream().filter(item1 ->
                                item1.getDeviceIp().equals(finalInstance)
                        ).collect(Collectors.toList());

                        if (collect.size() > 0) {
                            String deviceType = collect.get(0).getDeviceType();
                            promRespResult.setDeviceType(deviceType);
                            handleList.add(promRespResult);
                            if (handlerMap.containsKey(deviceType)) {
                                List<PromRespResult> promRespResults = handlerMap.get(deviceType);
                                promRespResults.add(promRespResult);
                                handlerMap.put(deviceType, promRespResults);
                            } else {
                                List<PromRespResult> promRespResults = new ArrayList<>();
                                promRespResults.add(promRespResult);
                                handlerMap.put(deviceType, promRespResults);
                            }
                        }
                    }
                }
            }
        }

        if (CollectionUtils.isEmpty(handleList)) {
            //return "没有要处理的数据";
            System.out.println("没有要处理的数据");
            return null;
        }
        System.out.println("数量总数：" + handleList.size());

        //统计在线的设备和离线设备
        AtomicReference<Integer> onlineDeviceNum = new AtomicReference<>(0);
        AtomicReference<Integer> offlineDeviceNum = new AtomicReference<>(0);
        AtomicReference<Long> currentTimestamp = new AtomicReference<>(0L);
        handleList.forEach(item -> {
            Object status = item.getValues().get(0).get(1);
            currentTimestamp.set(Long.valueOf(String.valueOf(item.getValues().get(0).get(0))));
            if (Objects.nonNull(status)) {
                String strStatus = (String) status;
                if ("1".equals(status)) {
                    onlineDeviceNum.set(onlineDeviceNum.get() + 1);
                } else {
                    offlineDeviceNum.set(offlineDeviceNum.get() + 1);
                }
            }
        });


        BigDecimal onlineRate = BigDecimal.valueOf(onlineDeviceNum.get()).divide(BigDecimal.valueOf(handleList.size()), 4, RoundingMode.HALF_UP);
        BigDecimal offlineRate = BigDecimal.valueOf(offlineDeviceNum.get()).divide(BigDecimal.valueOf(handleList.size()), 4, RoundingMode.HALF_UP);


        JSONObject jsonObject = new JSONObject();
        String time = DateUtils.formatDate(new Date(currentTimestamp.get() * 1000), DateUtils.DATETIME_FORMAT);
        jsonObject.put(time, "当前时间:" + time + ",在线数：" + onlineDeviceNum + ",离线数：" + offlineDeviceNum + ",在线率：" + onlineRate + ",离线率：" + offlineRate);
        respList.add(jsonObject);
        return respList;
    }


    /**
     * 服务器在线监控
     */
    @Override
    public List<Object> monitorServerDevice(StatisticDeviceDataDTO dto) {
        List<Object> respList = new ArrayList<>();
        //获取设备信息表的数据
        List<OmDeviceInfo> omDeviceInfos = omDeviceInfoMapper.selectAll();
        if (CollectionUtils.isEmpty(omDeviceInfos)) {
            return respList;
        }

        List<PromRespResult> handleList = new ArrayList<>();
        HashMap<String, List<PromRespResult>> handlerMap = new HashMap<>();
        List<String> statList = dto.getStatList();
        for (String item : statList) {
            //item = "{\"status\":\"success\",\"data\":{\"resultType\":\"matrix\",\"result\":[{\"metric\":{\"__name__\":\"up\",\"address\":\"10.252.176.74:9116\",\"devicename\":\"汇聚交换机\",\"instance\":\"10.52.116.1\",\"job\":\"snmp-exporter\",\"line_name\":\"十三号线\",\"station_code\":\"1310\",\"station_id\":\"293\",\"station_name\":\"官湖\",\"tenderSectionName\":\"E\"},\"values\":[[1721318400,\"0\"]]},{\"metric\":{\"__name__\":\"up\",\"address\":\"10.252.176.75:9116\",\"devicename\":\"汇聚交换机\",\"instance\":\"10.55.79.118\",\"job\":\"snmp-exporter\",\"line_name\":\"十三号线\",\"station_code\":\"1311\",\"station_id\":\"294\",\"station_name\":\"新沙\",\"tenderSectionName\":\"E\"},\"values\":[[1721318400,\"0\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.171.20:9100\",\"job\":\"node_exporter\",\"server\":\"开发平台服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.171.50:9100\",\"job\":\"node_exporter\",\"server\":\"存储服务ceph-01\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.113:9100\",\"job\":\"node_exporter\",\"server\":\"轨道交通-mysql8\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.15:9100\",\"job\":\"node_exporter\",\"server\":\"业务开发部-日志文件存储\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.17:19100\",\"job\":\"node_exporter\",\"server\":\"oracle-server k8s开发\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.18:19100\",\"job\":\"node_exporter\",\"server\":\"redis哨兵集群redis-sentinel-02\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.19:19100\",\"job\":\"node_exporter\",\"server\":\"redis哨兵集群redis-sentinel-03\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.22:9100\",\"job\":\"node_exporter\",\"server\":\"安检部使用-雷朋川2\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.24:9100\",\"job\":\"node_exporter\",\"server\":\"安检部使用-雷朋川1\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.2:9100\",\"job\":\"node_exporter\",\"server\":\"电子发票测试环境\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.30:9100\",\"job\":\"node_exporter\",\"server\":\"南大数据库-gbase\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.42:19100\",\"job\":\"node_exporter\",\"server\":\"测试部-测试环境\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.45:19100\",\"job\":\"node_exporter\",\"server\":\"nacos\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.46:9100\",\"job\":\"node_exporter\",\"server\":\"安检服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.50:19100\",\"job\":\"node_exporter\",\"server\":\"redis哨兵集群redis-sentinel-01\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.53:9100\",\"job\":\"node_exporter\",\"server\":\"智慧收益项目-帆软01\"},\"values\":[[1721318400,\"0\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.72:9100\",\"job\":\"node_exporter\",\"server\":\"智慧安检应用服务器-2\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.73:9100\",\"job\":\"node_exporter\",\"server\":\"智慧安检应用服务器-3\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.75:9100\",\"job\":\"node_exporter\",\"server\":\"智慧安检存储服务器-1\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.21.12:9100\",\"job\":\"node_exporter\",\"server\":\"大数据测试机-3(外部联调环境)\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.39.11:9100\",\"job\":\"node_exporter\",\"server\":\"elk日志与监控服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.39.18:9100\",\"job\":\"node_exporter\",\"server\":\"智慧安检集中判图项目\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.39.23:9100\",\"job\":\"node_exporter\",\"server\":\"安检部人脸项目使用-20210318\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.10:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.11:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.12:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.13:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.14:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.15:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.16:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.17:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.18:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"0\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.19:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"0\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.2:19100\",\"job\":\"node_exporter\",\"line_name\":\"十八号线\",\"server\":\"sims-jg01,sims-rf01,sims-sim01\",\"station_code\":\"9918\",\"station_id\":\"9918\",\"station_name\":\"横沥\",\"tenderSectionName\":\"E\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.3:19100\",\"job\":\"node_exporter\",\"line_name\":\"十三号线\",\"server\":\"sims-jg02,sims-im03\",\"station_code\":\"1308\",\"station_id\":\"291\",\"station_name\":\"白江\",\"tenderSectionName\":\"E\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.6:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.7:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.8:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.9:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]}]}}";
//            String path = JsonUtil.class.getClassLoader().getResource("query-server-demo.json").getPath();
//            String path = JsonUtil.class.getClassLoader().getResource("serverInfo-2024-08-08-05-30-00.json").getPath();
            String path = JsonUtil.class.getClassLoader().getResource("serverInfo-2024-08-14-02-00-00.json").getPath();
            item = JsonUtil.readJsonFile(path);

            PromDataDTO parseData = JSONObject.parseObject(item, PromDataDTO.class);
            System.out.println("解析后的json数据");
            //匹配ip的数据才统计处理
            if (Objects.nonNull(parseData.getData()) && !CollectionUtils.isEmpty(parseData.getData().getResult())) {
                List<PromRespResult> result = parseData.getData().getResult();
                for (PromRespResult promRespResult : result) {
                    if (Objects.nonNull(promRespResult.getMetric()) && !StringUtils.isEmpty(promRespResult.getMetric().getInstance())) {
                        String instance = promRespResult.getMetric().getInstance();
                        if (StringUtils.isEmpty(instance)) {
                            continue;
                        }
                        boolean contains = instance.contains(":");
                        if (contains) {
                            instance = promRespResult.getMetric().getInstance().split(":")[0];
                        }
                        String finalInstance = instance;
                        List<OmDeviceInfo> collect = omDeviceInfos.stream().filter(item1 ->
                                item1.getDeviceIp().equals(finalInstance)
                        ).collect(Collectors.toList());

                        if (collect.size() > 0) {
                            String deviceType = collect.get(0).getDeviceType();
                            promRespResult.setDeviceType(deviceType);
                            handleList.add(promRespResult);
                            if (handlerMap.containsKey(deviceType)) {
                                List<PromRespResult> promRespResults = handlerMap.get(deviceType);
                                promRespResults.add(promRespResult);
                                handlerMap.put(deviceType, promRespResults);
                            } else {
                                List<PromRespResult> promRespResults = new ArrayList<>();
                                promRespResults.add(promRespResult);
                                handlerMap.put(deviceType, promRespResults);
                            }
                        }
                    }
                }
            }
        }

        if (CollectionUtils.isEmpty(handleList)) {
            //return "没有要处理的数据";
            System.out.println("没有要处理的数据");
            return null;
        }
        System.out.println("数量总数：" + handleList.size());

        //统计在线的设备和离线设备
        AtomicReference<Integer> onlineDeviceNum = new AtomicReference<>(0);
        AtomicReference<Integer> offlineDeviceNum = new AtomicReference<>(0);
        AtomicReference<Long> currentTimestamp = new AtomicReference<>(0L);
        handleList.forEach(item -> {
            Object status = item.getValues().get(0).get(1);
            currentTimestamp.set(Long.valueOf(String.valueOf(item.getValues().get(0).get(0))));
            if (Objects.nonNull(status)) {
                String strStatus = (String) status;
                if ("1".equals(status)) {
                    onlineDeviceNum.set(onlineDeviceNum.get() + 1);
                } else {
                    offlineDeviceNum.set(offlineDeviceNum.get() + 1);
                }
            }
        });


        BigDecimal onlineRate = BigDecimal.valueOf(onlineDeviceNum.get()).divide(BigDecimal.valueOf(handleList.size()), 4, RoundingMode.HALF_UP);
        BigDecimal offlineRate = BigDecimal.valueOf(offlineDeviceNum.get()).divide(BigDecimal.valueOf(handleList.size()), 4, RoundingMode.HALF_UP);


        JSONObject jsonObject = new JSONObject();
        String time = DateUtils.formatDate(new Date(currentTimestamp.get() * 1000), DateUtils.DATETIME_FORMAT);
        jsonObject.put(time, "当前时间:" + time + ",在线数：" + onlineDeviceNum + ",离线数：" + offlineDeviceNum + ",在线率：" + onlineRate + ",离线率：" + offlineRate);
        respList.add(jsonObject);
        return respList;
    }


    /**
     * 服务在线监控
     */
    @Override
    public List<Object> monitorServiceDevice(StatisticDeviceDataDTO dto) {
        List<Object> respList = new ArrayList<>();
        //获取设备信息表的数据
        List<OmDeviceInfo> omDeviceInfos = omDeviceInfoMapper.selectAll();
        if (CollectionUtils.isEmpty(omDeviceInfos)) {
            return respList;
        }

        List<PromRespResult> handleList = new ArrayList<>();
        HashMap<String, List<PromRespResult>> handlerMap = new HashMap<>();
        List<String> statList = dto.getStatList();
        for (String item : statList) {
            //item = "{\"status\":\"success\",\"data\":{\"resultType\":\"matrix\",\"result\":[{\"metric\":{\"__name__\":\"up\",\"address\":\"10.252.176.74:9116\",\"devicename\":\"汇聚交换机\",\"instance\":\"10.52.116.1\",\"job\":\"snmp-exporter\",\"line_name\":\"十三号线\",\"station_code\":\"1310\",\"station_id\":\"293\",\"station_name\":\"官湖\",\"tenderSectionName\":\"E\"},\"values\":[[1721318400,\"0\"]]},{\"metric\":{\"__name__\":\"up\",\"address\":\"10.252.176.75:9116\",\"devicename\":\"汇聚交换机\",\"instance\":\"10.55.79.118\",\"job\":\"snmp-exporter\",\"line_name\":\"十三号线\",\"station_code\":\"1311\",\"station_id\":\"294\",\"station_name\":\"新沙\",\"tenderSectionName\":\"E\"},\"values\":[[1721318400,\"0\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.171.20:9100\",\"job\":\"node_exporter\",\"server\":\"开发平台服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.171.50:9100\",\"job\":\"node_exporter\",\"server\":\"存储服务ceph-01\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.113:9100\",\"job\":\"node_exporter\",\"server\":\"轨道交通-mysql8\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.15:9100\",\"job\":\"node_exporter\",\"server\":\"业务开发部-日志文件存储\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.17:19100\",\"job\":\"node_exporter\",\"server\":\"oracle-server k8s开发\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.18:19100\",\"job\":\"node_exporter\",\"server\":\"redis哨兵集群redis-sentinel-02\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.19:19100\",\"job\":\"node_exporter\",\"server\":\"redis哨兵集群redis-sentinel-03\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.22:9100\",\"job\":\"node_exporter\",\"server\":\"安检部使用-雷朋川2\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.24:9100\",\"job\":\"node_exporter\",\"server\":\"安检部使用-雷朋川1\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.2:9100\",\"job\":\"node_exporter\",\"server\":\"电子发票测试环境\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.30:9100\",\"job\":\"node_exporter\",\"server\":\"南大数据库-gbase\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.42:19100\",\"job\":\"node_exporter\",\"server\":\"测试部-测试环境\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.45:19100\",\"job\":\"node_exporter\",\"server\":\"nacos\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.46:9100\",\"job\":\"node_exporter\",\"server\":\"安检服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.50:19100\",\"job\":\"node_exporter\",\"server\":\"redis哨兵集群redis-sentinel-01\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.53:9100\",\"job\":\"node_exporter\",\"server\":\"智慧收益项目-帆软01\"},\"values\":[[1721318400,\"0\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.72:9100\",\"job\":\"node_exporter\",\"server\":\"智慧安检应用服务器-2\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.73:9100\",\"job\":\"node_exporter\",\"server\":\"智慧安检应用服务器-3\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.176.75:9100\",\"job\":\"node_exporter\",\"server\":\"智慧安检存储服务器-1\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.21.12:9100\",\"job\":\"node_exporter\",\"server\":\"大数据测试机-3(外部联调环境)\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.39.11:9100\",\"job\":\"node_exporter\",\"server\":\"elk日志与监控服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.39.18:9100\",\"job\":\"node_exporter\",\"server\":\"智慧安检集中判图项目\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.252.39.23:9100\",\"job\":\"node_exporter\",\"server\":\"安检部人脸项目使用-20210318\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.10:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.11:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.12:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.13:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.14:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.15:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.16:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.17:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.18:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"0\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.19:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"0\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.2:19100\",\"job\":\"node_exporter\",\"line_name\":\"十八号线\",\"server\":\"sims-jg01,sims-rf01,sims-sim01\",\"station_code\":\"9918\",\"station_id\":\"9918\",\"station_name\":\"横沥\",\"tenderSectionName\":\"E\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.3:19100\",\"job\":\"node_exporter\",\"line_name\":\"十三号线\",\"server\":\"sims-jg02,sims-im03\",\"station_code\":\"1308\",\"station_id\":\"291\",\"station_name\":\"白江\",\"tenderSectionName\":\"E\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.6:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.7:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.8:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]},{\"metric\":{\"__name__\":\"up\",\"instance\":\"10.53.5.9:19100\",\"job\":\"node_exporter\",\"server\":\"新运通云服务器\"},\"values\":[[1721318400,\"1\"]]}]}}";
            //String path = JsonUtil.class.getClassLoader().getResource("query-service-demo.json").getPath();
//            String path = JsonUtil.class.getClassLoader().getResource("service-2024-08-08-01-00-00.json").getPath();
            String path = JsonUtil.class.getClassLoader().getResource("service-2024-08-15-01-00-00.json").getPath();
            item = JsonUtil.readJsonFile(path);

            PromDataDTO parseData = JSONObject.parseObject(item, PromDataDTO.class);
            System.out.println("解析后的json数据");
            //匹配ip的数据才统计处理
            if (Objects.nonNull(parseData.getData()) && !CollectionUtils.isEmpty(parseData.getData().getResult())) {
                List<PromRespResult> result = parseData.getData().getResult();
                for (PromRespResult promRespResult : result) {
                    if (Objects.nonNull(promRespResult.getMetric()) && !StringUtils.isEmpty(promRespResult.getMetric().getInstance())) {
                        String instance = promRespResult.getMetric().getInstance();
                        if (StringUtils.isEmpty(instance)) {
                            continue;
                        }
                        boolean contains = instance.contains(":");
                        if (contains) {
                            instance = promRespResult.getMetric().getInstance().split(":")[0];
                        }
                        String finalInstance = instance;
                        List<OmDeviceInfo> collect = omDeviceInfos.stream().filter(item1 ->
                                item1.getDeviceIp().equals(finalInstance)
                        ).collect(Collectors.toList());

                        if (collect.size() > 0) {
                            String deviceType = collect.get(0).getDeviceType();
                            promRespResult.setDeviceType(deviceType);
                            handleList.add(promRespResult);
                            if (handlerMap.containsKey(deviceType)) {
                                List<PromRespResult> promRespResults = handlerMap.get(deviceType);
                                promRespResults.add(promRespResult);
                                handlerMap.put(deviceType, promRespResults);
                            } else {
                                List<PromRespResult> promRespResults = new ArrayList<>();
                                promRespResults.add(promRespResult);
                                handlerMap.put(deviceType, promRespResults);
                            }
                        }
                    }
                }
            }
        }

        if (CollectionUtils.isEmpty(handleList)) {
            //return "没有要处理的数据";
            System.out.println("没有要处理的数据");
            return null;
        }
        System.out.println("数量总数：" + handleList.size());

        //统计服务的在线和离线状态
        AtomicReference<Integer> onlineDeviceNum = new AtomicReference<>(0);
        AtomicReference<Integer> offlineDeviceNum = new AtomicReference<>(0);
        AtomicReference<Long> currentTimestamp = new AtomicReference<>(0L);
        handleList.forEach(item -> {
            Object status = item.getValues().get(0).get(1);
            currentTimestamp.set(Long.valueOf(String.valueOf(item.getValues().get(0).get(0))));
            if (Objects.nonNull(status)) {
                String strStatus = (String) status;
                if ("1".equals(status)) {
                    onlineDeviceNum.set(onlineDeviceNum.get() + 1);
                } else {
                    offlineDeviceNum.set(offlineDeviceNum.get() + 1);
                }
            }
        });


        BigDecimal onlineRate = BigDecimal.valueOf(onlineDeviceNum.get()).divide(BigDecimal.valueOf(handleList.size()), 6, RoundingMode.HALF_UP);
        BigDecimal offlineRate = BigDecimal.valueOf(offlineDeviceNum.get()).divide(BigDecimal.valueOf(handleList.size()), 6, RoundingMode.HALF_UP);


        JSONObject jsonObject = new JSONObject();
        String time = DateUtils.formatDate(new Date(currentTimestamp.get() * 1000), DateUtils.DATETIME_FORMAT);
        jsonObject.put(time, "当前时间:" + time + ",服务在线数：" + onlineDeviceNum + ",服务离线数：" + offlineDeviceNum + ",服务在线率：" + onlineRate + ",服务离线率：" + offlineRate);
        respList.add(jsonObject);
        return respList;

    }

    @Override
    public List<Object> monitorServiceDeviceNotMatch(StatisticDeviceDataDTO dto) {
        List<Object> respList = new ArrayList<>();
        //获取设备信息表的数据
        List<OmDeviceInfo> omDeviceInfos = omDeviceInfoMapper.selectAll();
        if (CollectionUtils.isEmpty(omDeviceInfos)) {
            return respList;
        }

        List<PromRespResult> handleList = new ArrayList<>();
        HashMap<String, List<PromRespResult>> handlerMap = new HashMap<>();
        List<String> statList = dto.getStatList();
        for (String item : statList) {
            String path = JsonUtil.class.getClassLoader().getResource("service-2024-08-15-01-00-00.json").getPath();
            item = JsonUtil.readJsonFile(path);

            PromDataDTO parseData = JSONObject.parseObject(item, PromDataDTO.class);
            System.out.println("解析后的json数据");
            //匹配ip的数据才统计处理
            if (Objects.nonNull(parseData.getData()) && !CollectionUtils.isEmpty(parseData.getData().getResult())) {
                List<PromRespResult> result = parseData.getData().getResult();
                for (PromRespResult promRespResult : result) {
                    if (Objects.nonNull(promRespResult.getMetric()) && !StringUtils.isEmpty(promRespResult.getMetric().getInstance())) {
                        String instance = promRespResult.getMetric().getInstance();
                        if (StringUtils.isEmpty(instance)) {
                            continue;
                        }
                        boolean contains = instance.contains(":");
                        if (contains) {
                            instance = promRespResult.getMetric().getInstance().split(":")[0];
                        }
                        String finalInstance = instance;
                        List<OmDeviceInfo> collect = omDeviceInfos.stream().filter(item1 ->
                                item1.getDeviceIp().equals(finalInstance)
                        ).collect(Collectors.toList());

                        //此时
                        handleList.add(promRespResult);

                        if (collect.size() > 0) {
                            String deviceType = collect.get(0).getDeviceType();
                            promRespResult.setDeviceType(deviceType);
                            //暂时注释
                            //handleList.add(promRespResult);
                            if (handlerMap.containsKey(deviceType)) {
                                List<PromRespResult> promRespResults = handlerMap.get(deviceType);
                                promRespResults.add(promRespResult);
                                handlerMap.put(deviceType, promRespResults);
                            } else {
                                List<PromRespResult> promRespResults = new ArrayList<>();
                                promRespResults.add(promRespResult);
                                handlerMap.put(deviceType, promRespResults);
                            }
                        }
                    }
                }
            }
        }

        if (CollectionUtils.isEmpty(handleList)) {
            //return "没有要处理的数据";
            System.out.println("没有要处理的数据");
            return null;
        }
        System.out.println("数量总数：" + handleList.size());

        //统计服务的在线和离线状态
        AtomicReference<Integer> onlineDeviceNum = new AtomicReference<>(0);
        AtomicReference<Integer> offlineDeviceNum = new AtomicReference<>(0);
        AtomicReference<Long> currentTimestamp = new AtomicReference<>(0L);
        handleList.forEach(item -> {
            Object status = item.getValues().get(0).get(1);
            currentTimestamp.set(Long.valueOf(String.valueOf(item.getValues().get(0).get(0))));
            if (Objects.nonNull(status)) {
                String strStatus = (String) status;
                if ("1".equals(status)) {
                    onlineDeviceNum.set(onlineDeviceNum.get() + 1);
                } else {
                    offlineDeviceNum.set(offlineDeviceNum.get() + 1);
                }
            }
        });


        BigDecimal onlineRate = BigDecimal.valueOf(onlineDeviceNum.get()).divide(BigDecimal.valueOf(handleList.size()), 6, RoundingMode.HALF_UP);
        BigDecimal offlineRate = BigDecimal.valueOf(offlineDeviceNum.get()).divide(BigDecimal.valueOf(handleList.size()), 6, RoundingMode.HALF_UP);


        JSONObject jsonObject = new JSONObject();
        String time = DateUtils.formatDate(new Date(currentTimestamp.get() * 1000), DateUtils.DATETIME_FORMAT);
        jsonObject.put(time, "当前时间:" + time + ",服务在线数：" + onlineDeviceNum + ",服务离线数：" + offlineDeviceNum + ",服务在线率：" + onlineRate + ",服务离线率：" + offlineRate);
        respList.add(jsonObject);
        return respList;
    }

    //统计光电口启用状态
    @Override
    public JSONObject statisticLightEntranceEnableStatus(String deviceIp) {
        //String path = JsonUtil.class.getClassLoader().getResource("query-ifAdminStatus-2024-10-08-19-57.json").getPath();
//        String path = JsonUtil.class.getClassLoader().getResource("query-ifAdminStaus-2024-10-9-10-24.json").getPath();
//        String path = JsonUtil.class.getClassLoader().getResource("query-ifAdminStaus-2024-10-9-13-35.json").getPath();
        String path = JsonUtil.class.getClassLoader().getResource("ifAdminStatus-query-2024-10-13-01-37.json").getPath();
//        String path = JsonUtil.class.getClassLoader().getResource("ifAdminStatus.json").getPath();
        String item = JsonUtil.readJsonFile(path);
        LightEnablePromDataDTO parseData = JSONObject.parseObject(item, LightEnablePromDataDTO.class);
        System.out.println("解析后的json数据");
        //集合数据
        HashMap<String, List<String>> resultMap = new HashMap<>();
        HashMap<String, String> resultNumberMap = new HashMap<>();
        if (Objects.nonNull(parseData.getData()) && !CollectionUtils.isEmpty(parseData.getData().getResult())) {
            List<PromRespLightEnableResult> result = parseData.getData().getResult();
            for (PromRespLightEnableResult promRespResult : result) {
                String instance = promRespResult.getMetric().getInstance();
                String lightNumber = promRespResult.getMetric().getIfDescr();
                if (StringUtils.isEmpty(instance)) {
                    continue;
                }
                boolean contains = instance.contains(":");
                if (contains) {
                    instance = promRespResult.getMetric().getInstance().split(":")[0];
                }
                String finalInstance = instance;
                if (!StringUtils.isEmpty(lightNumber) && !StringUtils.isEmpty(finalInstance)) {
                    if (resultMap.containsKey(finalInstance)) {
                        List<String> strings = resultMap.get(finalInstance);
                        strings.add(lightNumber);
                        resultMap.put(finalInstance, strings);
                    } else {
                        List<String> strings = new ArrayList<>();
                        strings.add(lightNumber);
                        resultMap.put(finalInstance, strings);
                    }
                }

                //启用状态存储容器
                //String enableStatus = (String) promRespResult.getValue().get(0).get(1);
                String enableStatus = (String) promRespResult.getValue().get(1);
                resultNumberMap.put(lightNumber, enableStatus);
            }

        }

        //获取关电口的设备的信息
        //获取设备信息表的数据
        List<OmSwitchPtInfo> omSwitchPtInfos = omSwitchPtInfoMapper.selectAll();
        if (CollectionUtils.isEmpty(omSwitchPtInfos)) {
            return new JSONObject();
        }


        JSONObject jsonObject = new JSONObject();
        for (Map.Entry<String, List<String>> entry : resultMap.entrySet()) {
            List<String> numberListValue = entry.getValue();
            String key = entry.getKey();
            if (CollectionUtils.isEmpty(numberListValue)) {
                continue;
            }


            //启用的 光口数 电口数 光电口数
            AtomicReference<Integer> enLightNum = new AtomicReference<>(0);
            AtomicReference<Integer> enElectricityNum = new AtomicReference<>(0);
            AtomicReference<Integer> enLightElectricityNum = new AtomicReference<>(0);

            //未启用的 光口数 电口数 光电口数
            AtomicReference<Integer> unLightNum = new AtomicReference<>(0);
            AtomicReference<Integer> unElectricityNum = new AtomicReference<>(0);
            AtomicReference<Integer> unLightElectricityNum = new AtomicReference<>(0);

            numberListValue.stream().forEach(numberItem -> {
                String enableStatus = resultNumberMap.get(numberItem);
                System.out.println("numberItem=" + numberItem + "   enableStatus=" + enableStatus);
                if (!StringUtils.isEmpty(enableStatus)) {
                    if (enableStatus.equals("1")) {
                        List<OmSwitchPtInfo> collect = omSwitchPtInfos.stream().filter(dbItem -> dbItem.getNumber().equals(numberItem)).collect(Collectors.toList());
                        if (!CollectionUtils.isEmpty(collect)) {
                            String type = collect.get(0).getType();
                            if (LightTypeEnum.LIGHT.getType().equals(type)) {
                                enLightNum.set(enLightNum.get() + 1);
                            }
                            if (LightTypeEnum.ELECTRICITY.getType().equals(type)) {
                                enElectricityNum.getAndSet(enElectricityNum.get() + 1);
                            }
                            if (LightTypeEnum.LIGHT_ELECTRICITY.getType().equals(type)) {
                                enLightElectricityNum.getAndSet(enLightElectricityNum.get() + 1);
                            }
                        }
                    }


                    if (enableStatus.equals("2")) {
                        List<OmSwitchPtInfo> collect = omSwitchPtInfos.stream().filter(dbItem -> dbItem.getNumber().equals(numberItem)).collect(Collectors.toList());
                        if (!CollectionUtils.isEmpty(collect)) {
                            String type = collect.get(0).getType();
                            if (LightTypeEnum.LIGHT.getType().equals(type)) {
                                unLightNum.set(unLightNum.get() + 1);
                            }
                            if (LightTypeEnum.ELECTRICITY.getType().equals(type)) {
                                unElectricityNum.getAndSet(unElectricityNum.get() + 1);
                            }
                            if (LightTypeEnum.LIGHT_ELECTRICITY.getType().equals(type)) {
                                unLightElectricityNum.getAndSet(unLightElectricityNum.get() + 1);
                            }
                        }
                    }
                }

            });
            if (!StringUtils.isEmpty(deviceIp) && key.equals(deviceIp)) {
                jsonObject.put(key, "en光口:" + enLightNum + "  en电口:" + enElectricityNum + " en光电口:" + enLightElectricityNum + " un光口:" + unLightNum + "  un电口:" + unElectricityNum + " un光电口:" + unLightElectricityNum);
                break;
            }
            if (StringUtils.isEmpty(deviceIp)) {
                jsonObject.put(key, "en光口:" + enLightNum + "  en电口:" + enElectricityNum + " en光电口:" + enLightElectricityNum + " un光口:" + unLightNum + "  un电口:" + unElectricityNum + " un光电口:" + unLightElectricityNum);
            }
            if (!StringUtils.isEmpty(deviceIp) && !key.equals(deviceIp)) {
                System.out.println(deviceIp + "输入的ip与db表中的数据不匹配");
                continue;
            }
        }

        return jsonObject;
    }

    //统计光电口在线离线状态
    @Override
    public JSONObject statisticLightEntranceOnlineStatus(String deviceIp) {
        //String path = JsonUtil.class.getClassLoader().getResource("query-ifOperStatus-2024-10-08-19-52.json").getPath();
        //String path = JsonUtil.class.getClassLoader().getResource("ifOperStatus.json").getPath();
        String path = JsonUtil.class.getClassLoader().getResource("query-ifOperStatus-2024-10-09-14-42-occ.json").getPath();
        String item = JsonUtil.readJsonFile(path);
        LightEnablePromDataDTO parseData = JSONObject.parseObject(item, LightEnablePromDataDTO.class);
        System.out.println("解析后的json数据");
        //集合数据
        HashMap<String, List<String>> resultMap = new HashMap<>();
        HashMap<String, String> resultNumberMap = new HashMap<>();
        if (Objects.nonNull(parseData.getData()) && !CollectionUtils.isEmpty(parseData.getData().getResult())) {
            List<PromRespLightEnableResult> result = parseData.getData().getResult();
            for (PromRespLightEnableResult promRespResult : result) {
                String instance = promRespResult.getMetric().getInstance();
                String lightNumber = promRespResult.getMetric().getIfDescr();
                if (StringUtils.isEmpty(instance)) {
                    continue;
                }
                boolean contains = instance.contains(":");
                if (contains) {
                    instance = promRespResult.getMetric().getInstance().split(":")[0];
                }
                String finalInstance = instance;
                if (!StringUtils.isEmpty(lightNumber) && !StringUtils.isEmpty(finalInstance)) {
                    if (resultMap.containsKey(finalInstance)) {
                        List<String> strings = resultMap.get(finalInstance);
                        strings.add(lightNumber);
                        resultMap.put(finalInstance, strings);
                    } else {
                        List<String> strings = new ArrayList<>();
                        strings.add(lightNumber);
                        resultMap.put(finalInstance, strings);
                    }
                }

                //启用状态存储容器
                //String enableStatus = (String) promRespResult.getValue().get(0).get(1);
                String enableStatus = (String) promRespResult.getValue().get(1);
                resultNumberMap.put(lightNumber, enableStatus);
            }

        }

        //获取关电口的设备的信息
        //获取设备信息表的数据
        List<OmSwitchPtInfo> omSwitchPtInfos = omSwitchPtInfoMapper.selectAll();
        if (CollectionUtils.isEmpty(omSwitchPtInfos)) {
            return new JSONObject();
        }


        JSONObject jsonObject = new JSONObject();
        for (Map.Entry<String, List<String>> entry : resultMap.entrySet()) {
            List<String> numberListValue = entry.getValue();
            String key = entry.getKey();
            if (CollectionUtils.isEmpty(numberListValue)) {
                continue;
            }


            //启用的 光口数 电口数 光电口数
            AtomicReference<Integer> enLightNum = new AtomicReference<>(0);
            AtomicReference<Integer> enElectricityNum = new AtomicReference<>(0);
            AtomicReference<Integer> enLightElectricityNum = new AtomicReference<>(0);

            //未启用的 光口数 电口数 光电口数
            AtomicReference<Integer> unLightNum = new AtomicReference<>(0);
            AtomicReference<Integer> unElectricityNum = new AtomicReference<>(0);
            AtomicReference<Integer> unLightElectricityNum = new AtomicReference<>(0);

            //在线和离线数量
            AtomicReference<Integer> onlineNum = new AtomicReference<>(0);
            AtomicReference<Integer> offlineNum = new AtomicReference<>(0);

            ArrayList<String> onlineList = new ArrayList<>();
            ArrayList<String> offlineList = new ArrayList<>();

            numberListValue.stream().forEach(numberItem -> {
                String enableStatus = resultNumberMap.get(numberItem);
                if (!StringUtils.isEmpty(enableStatus)) {
                    if (enableStatus.equals("1")) {
                        List<OmSwitchPtInfo> collect = omSwitchPtInfos.stream().filter(dbItem -> dbItem.getNumber().equals(numberItem)).collect(Collectors.toList());
                        if (!CollectionUtils.isEmpty(collect)) {
                            onlineNum.getAndSet(onlineNum.get() + 1);
                            onlineList.add(numberItem);
                            String type = collect.get(0).getType();
                            if (LightTypeEnum.LIGHT.getType().equals(type)) {
                                enLightNum.set(enLightNum.get() + 1);
                            }
                            if (LightTypeEnum.ELECTRICITY.getType().equals(type)) {
                                enElectricityNum.getAndSet(enElectricityNum.get() + 1);
                            }
                            if (LightTypeEnum.LIGHT_ELECTRICITY.getType().equals(type)) {
                                enLightElectricityNum.getAndSet(enLightElectricityNum.get() + 1);
                            }
                        }
                    }


                    if (enableStatus.equals("2")) {
                        List<OmSwitchPtInfo> collect = omSwitchPtInfos.stream().filter(dbItem -> dbItem.getNumber().equals(numberItem)).collect(Collectors.toList());
                        if (!CollectionUtils.isEmpty(collect)) {
                            offlineNum.getAndSet(offlineNum.get() + 1);
                            offlineList.add(numberItem);
                            String type = collect.get(0).getType();
                            if (LightTypeEnum.LIGHT.getType().equals(type)) {
                                unLightNum.set(unLightNum.get() + 1);
                            }
                            if (LightTypeEnum.ELECTRICITY.getType().equals(type)) {
                                unElectricityNum.getAndSet(unElectricityNum.get() + 1);
                            }
                            if (LightTypeEnum.LIGHT_ELECTRICITY.getType().equals(type)) {
                                unLightElectricityNum.getAndSet(unLightElectricityNum.get() + 1);
                            }
                        }
                    }
                }

            });
            if (!StringUtils.isEmpty(deviceIp) && key.equals(deviceIp)) {
                // jsonObject.put(key, "en光口:" + enLightNum + "  en电口:" + enElectricityNum + " en光电口:" + enLightElectricityNum + " un光口:" + unLightNum + "  un电口:" + unElectricityNum + " un光电口:" + unLightElectricityNum);
//                jsonObject.put("ONLINE", "在线接口编号:" + JSONObject.toJSONString(onlineList));
//                jsonObject.put("OFFLINE", "离线接口编号:" + JSONObject.toJSONString(offlineList));
                jsonObject.put(key, "在线接口编号:" + JSONObject.toJSONString(onlineList) + "  离线接口编号:" + JSONObject.toJSONString(offlineList) + "总数量=" + (onlineList.size() + offlineList.size()) + " onlineList=" + onlineList.size() + " offlineList=" + offlineList.size());
                break;
            }
            if (StringUtils.isEmpty(deviceIp)) {
                jsonObject.put(key, "在线接口编号:" + JSONObject.toJSONString(onlineList) + "  离线接口编号:" + JSONObject.toJSONString(offlineList) + "总数量=" + (onlineList.size() + offlineList.size()) + " onlineList=" + onlineList.size() + " offlineList=" + offlineList.size());
//                jsonObject.put("ONLINE", "在线接口编号:" + JSONObject.toJSONString(onlineList));
//                jsonObject.put("OFFLINE", "离线接口编号:" + JSONObject.toJSONString(offlineList));
            }
            if (!StringUtils.isEmpty(deviceIp) && !key.equals(deviceIp)) {
                System.out.println(deviceIp + "输入的ip与db表中的数据不匹配");
                continue;
            }
        }

        return jsonObject;
    }

    //统计node-exporter的数据 stationCode：必填
    @Override
    public JSONObject statisticNodeExporterTarget(String stationId, String stationCode) {
        String path = JsonUtil.class.getClassLoader().getResource("targets-line-server-2024-21-09.json").getPath();
//        String path = JsonUtil.class.getClassLoader().getResource("demo-targets-line-server-2024-21-09.json").getPath();
        String item = JsonUtil.readJsonFile(path);
        NodeExporterTargetResp parseData = JSONObject.parseObject(item, NodeExporterTargetResp.class);
        System.out.println("解析后的json数据");
        List<ActiveTarget> activeTargets = parseData.getData().getActiveTargets();
        HashMap<String, List<NodeExporterLabel>> upMap = new HashMap<>();
        HashMap<String, List<NodeExporterLabel>> downMap = new HashMap<>();


        String upKey = "up";
        String downKey = "down";

        //获取设备信息表的数据
        List<OmDeviceInfo> omDeviceInfos = omDeviceInfoMapper.selectAll();
        System.out.println("获取设备信息表的数据size=" + omDeviceInfos.size());

        ///根据station_code 汇总并去重

        long upCount = 0;
        long downCount = 0;
        //判断是否重复统计
        HashMap<String, String> existStatisticMap = new HashMap<>();

        for (OmDeviceInfo omDeviceInfo : omDeviceInfos) {
            //限制条件
            if (!stationCode.equals(omDeviceInfo.getLineId())) {
                continue;
            }
            String deviceIp = omDeviceInfo.getDeviceIp();
            for (ActiveTarget activeTarget : activeTargets) {
                NodeExporterLabel labels = activeTarget.getLabels();
                String instance = labels.getInstance();
                boolean contains = instance.contains(":");
                if (contains) {
                    //instance变成ip
                    instance = instance.split(":")[0];
                }
                if (deviceIp.equals(instance) && stationCode.equals(labels.getStation_code()) ) {
                    //判断是否重复统计
                    String flag = existStatisticMap.get(instance);
                    if (StringUtils.isEmpty(flag)) {
                        String health = activeTarget.getHealth();
                        if ("up".equals(health)) {
                            upCount = upCount + 1;
                        }
                        if ("down".equals(health)) {
                            downCount = downCount + 1;
                        }
                        existStatisticMap.put(instance,"1");
                        break;
                    } else {
                        System.out.println("Key重复");
                    }
                }
            }
        }


        JSONObject jsonObject = new JSONObject();
        jsonObject.put(stationCode, "总数量=" + (upCount + downCount) + " 在线数量=" + upCount + " 离线数量=" + downCount);
        return jsonObject;
    }
  /*  public JSONObject statisticNodeExporterTargetDraft(String stationId, String stationCode) {
        String path = JsonUtil.class.getClassLoader().getResource("targets-line-server-2024-21-09.json").getPath();
//        String path = JsonUtil.class.getClassLoader().getResource("demo-targets-line-server-2024-21-09.json").getPath();
        String item = JsonUtil.readJsonFile(path);
        NodeExporterTargetResp parseData = JSONObject.parseObject(item, NodeExporterTargetResp.class);
        System.out.println("解析后的json数据");
        List<ActiveTarget> activeTargets = parseData.getData().getActiveTargets();
        HashMap<String, List<NodeExporterLabel>> upMap = new HashMap<>();
        HashMap<String, List<NodeExporterLabel>> downMap = new HashMap<>();

        String upKey = "up";
        String downKey = "down";
        for (ActiveTarget activeTarget : activeTargets) {
            if (Objects.nonNull(activeTarget.getLabels()) && !StringUtils.isEmpty(activeTarget.getHealth()) && activeTarget.getHealth().equals(upKey)) {
                if (upMap.containsKey(upKey)) {
                    List<NodeExporterLabel> strings = upMap.get(upKey);
                    strings.add(activeTarget.getLabels());
                    upMap.put(upKey, strings);
                } else {
                    List<NodeExporterLabel> strings = new ArrayList<>();
                    strings.add(activeTarget.getLabels());
                    upMap.put(upKey, strings);
                }
            }


            if (Objects.nonNull(activeTarget.getLabels()) && !StringUtils.isEmpty(activeTarget.getHealth()) && activeTarget.getHealth().equals(downKey)) {
                if (downMap.containsKey(downKey)) {
                    List<NodeExporterLabel> strings = downMap.get(downKey);
                    strings.add(activeTarget.getLabels());
                    downMap.put(downKey, strings);
                } else {
                    List<NodeExporterLabel> strings = new ArrayList<>();
                    strings.add(activeTarget.getLabels());
                    downMap.put(downKey, strings);
                }
            }

        }

        //获取设备信息表的数据
        List<OmDeviceInfo> omDeviceInfos = omDeviceInfoMapper.selectAll();
        System.out.println("获取设备信息表的数据size=" + omDeviceInfos.size());

        ///根据station_code 汇总并去重
        List<NodeExporterLabel> nodeExporterLabels = upMap.get(upKey);
        List<NodeExporterLabel> downNodeExporterLabels = downMap.get(downKey);

        long upCount = 0;
        long downCount = 0;
        //判断是否重复统计
        HashMap<String, String> existStatisticMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(nodeExporterLabels)) {

            //upCount = nodeExporterLabels.stream().filter(nodeExporterLabelsItem -> stationCode.equals(nodeExporterLabelsItem.getStation_code())).map(NodeExporterLabel::getInstance).distinct().count();
            System.out.println("在线的数量=" + nodeExporterLabels.size());
            for (NodeExporterLabel nodeExporterLabel : nodeExporterLabels) {
                if (stationCode.equals(nodeExporterLabel.getStation_code())) {

                    String instance = nodeExporterLabel.getInstance();
                    if (!StringUtils.isEmpty(instance)) {
                        boolean contains = instance.contains(":");
                        if (contains) {
                            //instance变成ip
                            instance = instance.split(":")[0];
                        }
                        String finalInstance = instance;
                        List<OmDeviceInfo> collect = omDeviceInfos.stream().filter(omDeviceInfo -> omDeviceInfo.getDeviceIp().equals(finalInstance)).collect(Collectors.toList());
                        if (!CollectionUtils.isEmpty(collect)) {
                            //判断是否重复统计
                            String flag = existStatisticMap.get(upKey + finalInstance);
                            if (StringUtils.isEmpty(flag)) {
                                upCount = upCount + 1;
                                existStatisticMap.put(upKey + finalInstance, "1");
                            } else {
                                System.out.println("upKey重复");
                            }
                        }
                    }
                }
            }
        }
        if (!CollectionUtils.isEmpty(downNodeExporterLabels)) {
            //downCount = downNodeExporterLabels.stream().filter(nodeExporterLabelsItem -> stationCode.equals(nodeExporterLabelsItem.getStation_code())).map(NodeExporterLabel::getInstance).distinct().count();
            System.out.println("离线的数量=" + downNodeExporterLabels.size());

            for (NodeExporterLabel nodeExporterLabel : downNodeExporterLabels) {
                if (stationCode.equals(nodeExporterLabel.getStation_code())) {
                    String instance = nodeExporterLabel.getInstance();
                    if (!StringUtils.isEmpty(instance)) {
                        boolean contains = instance.contains(":");
                        if (contains) {
                            //instance变成ip
                            instance = instance.split(":")[0];
                        }
                        String finalInstance = instance;
                        List<OmDeviceInfo> collect = omDeviceInfos.stream().filter(omDeviceInfo -> omDeviceInfo.getDeviceIp().equals(finalInstance)).collect(Collectors.toList());
                        if (!CollectionUtils.isEmpty(collect)) {
                            //判断是否重复统计
                            String flag = existStatisticMap.get(downKey + finalInstance);
                            if (StringUtils.isEmpty(flag)) {
                                downCount = downCount + 1;
                                existStatisticMap.put(downKey + finalInstance, "0");
                            } else {
                                System.out.println("downKey重复");
                            }
                        }
                    }
                }
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(stationCode, "总数量=" + (upCount + downCount) + " 在线数量=" + upCount + " 离线数量=" + downCount);
        return jsonObject;
    }*/


}
